//
//  VRBindDeviceViewController.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/19.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRBindDeviceTypeViewController.h"
#import "VRBindDeviceViewController.h"

#import "VRScrollView.h"

#import "UIImage+VR.h"


@interface VRBindDeviceTypeViewController ()

@property (nonatomic, strong) VRScrollView *contentScrollView1;
@property (nonatomic, strong) UIView *contentView;

@end

@implementation VRBindDeviceTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ScrollViewBgColor;
    self.title = @"设备类型";
    
    [self setupSubView];
}

- (void)setupSubView
{
    UIView *contentView = [[UIView alloc] init];
    [self.contentScrollView1 addSubview:contentView];
    self.contentView = contentView;
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentScrollView1);
        make.width.mas_equalTo(self.contentScrollView1);
    }];
    
    
    CGFloat centerMargin = 25 * ScreenScale;
    CGFloat itemWH = 128 * ScreenScale;
    CGFloat leftMargin = (kScreenWidth - 2 * itemWH - centerMargin) * 0.5;
    
    NSArray *imgNameArray = @[@"equipment_001", @"equipment_003", @"equipment_005", @"equipment_004", @""];
    NSArray *titleArray = @[@"烟", @"气", @"水", @"电", @"更 多..."];
    UIButton *lastBtn = nil;
    for (int i = 0; i < 5; i++) {
        
        CGFloat itemX = leftMargin + (itemWH + centerMargin) * (i % 2);
        CGFloat itemY = centerMargin + (itemWH + centerMargin) * (i / 2);
        UIButton *itemBtn = [self createitemWithImageName:imgNameArray[i] title:titleArray[i] size:CGSizeMake(itemWH, itemWH)];
        itemBtn.tag = i;
        itemBtn.x = itemX;
        itemBtn.y = itemY;
        [contentView addSubview:itemBtn];
        [itemBtn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        lastBtn = itemBtn;
    }
    
    [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(lastBtn).offset(20);
    }];
}


- (UIButton *)createitemWithImageName:(NSString *)imageName title:(NSString *)title size:(CGSize)size
{
    UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    itemBtn.layer.cornerRadius = 10;
    itemBtn.layer.borderWidth = 1;
    itemBtn.layer.borderColor = MAINColor.CGColor;
    itemBtn.layer.masksToBounds = YES;
    itemBtn.backgroundColor = [UIColor whiteColor];
    [itemBtn setBackgroundImage:[UIImage imageWithColor:RGBA(13, 101, 158, 0.15)] forState:UIControlStateHighlighted];
    itemBtn.size = size;
    
    UIImageView *headerIconImgView = [[UIImageView alloc] init];
    headerIconImgView.contentMode = UIViewContentModeScaleAspectFit;
    headerIconImgView.image = [UIImage imageNamed:imageName];
    [itemBtn addSubview:headerIconImgView];
    [headerIconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(itemBtn);
        make.centerY.mas_equalTo(itemBtn).offset(-17);
        make.width.height.mas_equalTo(70 * ScreenScale);
    }];
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = title;
    titleLbl.textColor = MAINColor;
    titleLbl.font = VRFont(16);
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [itemBtn addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(headerIconImgView);
        if (imageName.length > 0) {
            make.top.mas_equalTo(headerIconImgView.mas_bottom).offset(18);
        }else{
            make.centerY.mas_equalTo(itemBtn).offset(8);
        }
    }];
    
    return itemBtn;
}


#pragma mark - action
- (void)itemBtnClick:(UIButton *)sender
{
    if (sender.tag == 4) {
        [SVProgressHUD showImage:nil status:@"更多设备正在准备中！"];
        return;
    }
    
    VRBindDeviceViewController *bindVc = [[VRBindDeviceViewController alloc] init];
    bindVc.type = sender.tag;
    [self.navigationController pushViewController:bindVc animated:YES];
}


#pragma mark - lazy
- (VRScrollView *)contentScrollView1
{
    if (_contentScrollView1 == nil) {
        
        _contentScrollView1 = [[VRScrollView alloc] initWithFrame:self.view.bounds];
//        _contentScrollView1.backgroundColor = [UIColor whiteColor];
        //        _contentScrollView1.delegate = self;
        //        _contentScrollView.bounces = NO;
        _contentScrollView1.alwaysBounceVertical = YES;
        //        _contentScrollView.delegate = self;
        if (@available(iOS 11.0, *)) {
            //            _contentScrollView1.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [self.view addSubview:_contentScrollView1];
    }
    return _contentScrollView1;
}

@end
